package com.sgb.kjwl.viewmodel.self;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.BuildConfig;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.UrlConfig;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.self.ApprovalListEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.self.ApprovalManagerFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ApprovalModel extends AndroidViewModel {

    private  Map<String,Object> map = new HashMap<>();

    public ApprovalModel(@NonNull Application application) {
        super(application);
    }


    //待办事项
    public void getApplyPendingList(ApprovalManagerFragment fragment, int page, String type){
        APIManager.getInstance().changeAPIManager(BuildConfig.BASE_URL_DESIGN);
        DialogHelper.showProgressDialog(fragment.getContext(), "加载中", 0, false);
        map.put("companyId", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO,""));
        map.put("pageNum",page);
        map.put("pageSize",10);
        map.put("userId", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO,""));
        map.put("type", type);//审批类型
        map.put("initiator","");
        map.put("platformType","jyb");
        APIManager.getInstance().getUserAPI().getApplyPendingList(map).enqueue(new ICallBack<BaseEntity<ProListEntity<List<ApprovalListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<ApprovalListEntity>>> data) {
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getData() == null )
                    return;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.refreshUIByData(data.getData());
                }else{
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<ApprovalListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                fragment.refreshUIByData(null);
            }

            @Override
            public void onFinally() {
                super.onFinally();
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
            }
        });
    }
    //我发起的
    public void getQueryApproverListOfMine(ApprovalManagerFragment fragment, int page, String type){
        APIManager.getInstance().changeAPIManager(BuildConfig.BASE_URL_DESIGN);
        DialogHelper.showProgressDialog(fragment.getContext(), "加载中", 0, false);
        map.put("companyId",SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO,""));
        map.put("pageNum",page);
        map.put("pageSize",10);
        map.put("userId",SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO,""));
        map.put("status",-1);
        map.put("type","");//审批类型
        map.put("initiator", type);
        map.put("platformType","jyb");
        APIManager.getInstance().getUserAPI().getQueryApproverListOfMine(map).enqueue(new ICallBack<BaseEntity<ProListEntity<List<ApprovalListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<ApprovalListEntity>>> data) {
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getData() == null)
                    return;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.refreshUIByData(data.getData());
                }else{
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<ApprovalListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                fragment.refreshUIByData(null);
            }

            @Override
            public void onFail() {

            }

            @Override
            public void onFinally() {
                super.onFinally();
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
            }
        });
    }
    //已审批
    public void getApplyFinishedList(ApprovalManagerFragment fragment, int page, String type){
        APIManager.getInstance().changeAPIManager(BuildConfig.BASE_URL_DESIGN);
        DialogHelper.showProgressDialog(fragment.getContext(), "加载中", 0, false);
        map.put("companyId", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO,""));
        map.put("pageNum", page);
        map.put("pageSize", 10);
        map.put("userId", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO,""));
        map.put("type", type);//审批类型
        map.put("initiator","");
        map.put("platformType","jyb");
        APIManager.getInstance().getUserAPI().getApplyFinishedList(map).enqueue(new ICallBack<BaseEntity<ProListEntity<List<ApprovalListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<ApprovalListEntity>>> data) {
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getData() == null )
                    return;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.refreshUIByData(data.getData());
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<ApprovalListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                fragment.refreshUIByData(null);
            }

            @Override
            public void onFail() {
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
            }

            @Override
            public void onFinally() {
                super.onFinally();
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
            }
        });
    }
    //抄送我
    public void getNotifyListOfMine(ApprovalManagerFragment fragment,int page, String type){
        APIManager.getInstance().changeAPIManager(BuildConfig.BASE_URL_DESIGN);
        DialogHelper.showProgressDialog(fragment.getContext(), "加载中", 0, false);
        map.put("companyId", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO,""));
        map.put("pageNum", page);
        map.put("pageSize", 10);
        map.put("userId", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO,""));
        map.put("type", type);
        map.put("initiator","");
        map.put("platformType","jyb");
        APIManager.getInstance().getUserAPI().getNotifyListOfMine(map).enqueue(new ICallBack<BaseEntity<ProListEntity<List<ApprovalListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<ApprovalListEntity>>> data) {
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getData() == null )
                    return;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.refreshUIByData(data.getData());
                }else{
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<ApprovalListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                fragment.refreshUIByData(null);
            }

            @Override
            public void onFail() {
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
            }

            @Override
            public void onFinally() {
                super.onFinally();
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
            }
        });
    }


    //审批H5详情
    public void getUrl(ApprovalManagerFragment fragment,String objId,String detail,String showOperation,String auditId,int status){
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getTeamPriceAPI().getUrlSetting(detail).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                String url="";
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    switch (detail){
                        //租赁发布相关
                        case "mechanicsDetail":
                        case "equipmentDetails":
                        case "vehicleDetails":
                        case "propertyDetails":
                        case "landDetails":
                        case "factoryDetails":
                        case "farmDetail":
                            if(!TextUtils.isEmpty(showOperation)) {
                                url = response.body().getData().getUrl() + "?id=" + objId + "&iosFlag=3&auditId=" + auditId + "&showOperation=" + showOperation+"&status="+status;
                            }else{
                                url = response.body().getData().getUrl() + "?id=" + objId + "&iosFlag=3&auditId=" + auditId+"&status="+status;
                            }
                            break;
                        case "receivedOrderApproval"://租入订单确认,付款
                            url = response.body().getData().getUrl() + "?id=" + objId + "&iosFlag=3&auditId=" + auditId + "&showOperation=" + showOperation+"&showStatus="+status;
                            break;
                        //会员经营相关
                        case "approvalDetail"://申请，邀请加盟企业/会员解除审批
                        case "memberEvaluate":
                        //工队
                        case "workquotionStatus":
                        case "teamApproval":
                            if(!TextUtils.isEmpty(showOperation)) {
                                url = response.body().getData().getUrl() + "?no=" + objId + "&auditId=" + auditId + "&showOperation=" + showOperation+"&status="+status;
                            }else{
                                url = response.body().getData().getUrl() + "?no=" + objId + "&auditId=" + auditId+"&status="+status;
                            }
                            break;
                        case "hair":
                        case "Calibration":
                        case "Scrap":
                        case "repair":
                        case "bidapproval":
                        case "Abandoned":
                        case "Winning":
                        case "approvalAddendum":
                            if (!TextUtils.isEmpty(showOperation)){
                                url = response.body().getData().getUrl() + "?id=" + objId + "&auditId=" + auditId +  "&showOperation=" + showOperation+"&status="+status;
                            }else {
                                url = response.body().getData().getUrl() + "?id=" + objId + "&auditId=" + auditId+ "&showOperation=" + "0"+"&status="+status;
                            }
                            break;
                    }
                    WebViewX5HostActivity.start(fragment.getActivity(),url);
                    LogUtil.v("detailUrl=", url);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    //全部代办
    public void getAllList(ApprovalManagerFragment fragment, int page, String type){
        DialogHelper.showProgressDialog(fragment.getContext(), "加载中", 0, false);
        map.put("companyId", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO,""));
        map.put("pageNum", page);
        map.put("pageSize", 10);
        map.put("userId", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO,""));
        map.put("type", type);
        map.put("initiator","");
        map.put("platformType","jyb");
        APIManager.getInstance().getUserAPI().getAllListOfMine(map).enqueue(new ICallBack<BaseEntity<ProListEntity<List<ApprovalListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<ApprovalListEntity>>> data) {
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getData() == null )
                    return;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.refreshUIByData(data.getData());
                }else{
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<ApprovalListEntity>>>> call, Throwable t) {
                fragment.refreshUIByData(null);
            }

            @Override
            public void onFail() {
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
            }

            @Override
            public void onFinally() {
                super.onFinally();
                APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
            }
        });
    }
}
